<template>
  <view>
    <view class="gradient-bg">
      <!-- <image :src="myBgSrc" class="wdbg" mode="cover"></image> -->
      <view class style="width: calc(100% - 60rpx);margin: 0rpx auto;">
        <view class="big-line prow30">
          <view class="row-line flex justify-between">
            <text>新消息提醒</text>
            <view class="flex items-center">
              <text class="pr20">允许通知</text>
              <u-switch v-model="checkedIsPushMsg" :loading="msgLoading" active-color="#1FB0AD" @change="changePushMsg"></u-switch>
            </view>
          </view>
          <view class="row-line flex justify-between">
            <text>被关注消息</text>
            <view class="flex items-center">
              <text class="pr20">允许通知</text>
              <u-switch v-model="checkedIsPushFollow" active-color="#1FB0AD" @change="changePushFollow"></u-switch>
            </view>
          </view>
          <view class="row-line flex justify-between">
            <text>报价推送通知</text>
            <view class="flex items-center">
              <text class="pr20">允许通知</text>
              <u-switch v-model="checkedIsPushNews" active-color="#1FB0AD" @change="changePushPriceNews"></u-switch>
            </view>
          </view>
        </view>
        <!-- <view @click="confirm" style="width: 690rpx;height: 90rpx;background: #1FB0AC;border-radius: 49rpx;line-height: 90rpx;text-align: center;color:#ffffff;font-size: 32rpx;font-weight: 600;margin:64rpx auto 0;">
					确定
        </view>-->
      </view>
    </view>
    <callPhone ref="callPhone" />
    <pushPermigetssion ref="pushPermigetssion" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      myBgSrc: require('@/static/my/bg1.png'),
      myphoneSrc: require('@/static/my/ic_contact.png'),
      mywhatSrc: require('@/static/my/ic_link.png'),
      background: { background: 'linear-gradient(180deg, #0078FF 0%, #1fb0ac 100%)' },
      work: {},
      list: [],
      mobile: '',
      checkedIsPushMsg: true,
      checkedIsPushNews: true,
      checkedIsPushFollow: true,
      msgLoading: false,
      followLoading: false,
      priceLoading: false
    }
  },
  async onShow() {
    this.checkedIsPushFollow = (+uni.getStorageSync('is_push_follow') === 1) ? true : false
    this.checkedIsPushNews = (+uni.getStorageSync('is_push_news') === 1) ? true : false
    this.checkedIsPushMsg = (+uni.getStorageSync('is_push_msg') === 1) ? true : false
    let result = await this.$refs.pushPermigetssion.getQuanxian()
    if (result) {
      this.checkedIsPushFollow = true
      this.checkedIsPushNews = true
      this.checkedIsPushMsg = true
      this.changePushMsg1(true, false)
      this.changePushPriceNews1(true, false)
      this.changePushFollow1(true, false)
    } else {
      this.checkedIsPushFollow = false
      this.checkedIsPushNews = false
      this.checkedIsPushMsg = false
      this.changePushMsg1(false, false)
      this.changePushPriceNews1(false, false)
      this.changePushFollow1(false, false, false)
    }

  },
  onLoad() {
    this.wenti()
    this.dian()
    // this.wo()
  },
  onPullDownRefresh() {
    this.wenti()
    // this.wo()
    setTimeout(function () {
      uni.stopPullDownRefresh();
    }, 1500);
  },
  methods: {
    changePushMsg(val, bol = true) {
      if (this.msgLoading) return
      console.log('%c [ val ]-78', 'font-size:13px; background:pink; color:#bf2c9f;', val)
      this.msgLoading = true
      let data = {
        type: 'msg',
        val: val ? 1 : 0
      }
      this.post('/api/client/pushSet', data, bol).then(res => {
        if (res.code == 1) {
          this.$u.toast('设置成功')
          uni.setStorageSync('is_push_msg', data.val)
        }
        this.msgLoading = false
      })
    },
    changePushPriceNews(val, bol = true) {
      if (this.priceLoading) return
      console.log('%c [ val ]-78', 'font-size:13px; background:pink; color:#bf2c9f;', val)
      this.priceLoading = true
      let data = {
        type: 'news',
        val: val ? 1 : 0
      }
      this.post('/api/client/pushSet', data, bol).then(res => {
        if (res.code == 1) {
          this.$u.toast('设置成功')
          uni.setStorageSync('is_push_news', data.val)
        }
        this.priceLoading = false
      })
    },
    changePushFollow(val, bol = true) {
      if (this.followLoading) return
      console.log('%c [ val ]-78', 'font-size:13px; background:pink; color:#bf2c9f;', val)
      this.followLoading = true
      let data = {
        type: 'follow',
        val: val ? 1 : 0
      }
      this.post('/api/client/pushSet', data, bol).then(res => {
        if (res.code == 1) {
          this.$u.toast('设置成功')
          uni.setStorageSync('is_push_follow', data.val)
        }
        this.followLoading = false
      })
    },
    changePushMsg1(val, bol = true) {
      if (this.msgLoading) return
      console.log('%c [ val ]-78', 'font-size:13px; background:pink; color:#bf2c9f;', val)
      this.msgLoading = true
      let data = {
        type: 'msg',
        val: val ? 1 : 0
      }
      this.post('/api/client/pushSet', data, bol).then(res => {
        if (res.code == 1) {
          uni.setStorageSync('is_push_msg', data.val)
        }
        this.msgLoading = false
      })
    },
    changePushPriceNews1(val, bol = true) {
      if (this.priceLoading) return
      console.log('%c [ val ]-78', 'font-size:13px; background:pink; color:#bf2c9f;', val)
      this.priceLoading = true
      let data = {
        type: 'news',
        val: val ? 1 : 0
      }
      this.post('/api/client/pushSet', data, bol).then(res => {
        if (res.code == 1) {
          uni.setStorageSync('is_push_news', data.val)
        }
        this.priceLoading = false
      })
    },
    changePushFollow1(val, bol = true) {
      if (this.followLoading) return
      console.log('%c [ val ]-78', 'font-size:13px; background:pink; color:#bf2c9f;', val)
      this.followLoading = true
      let data = {
        type: 'follow',
        val: val ? 1 : 0
      }
      this.post('/api/client/pushSet', data, bol).then(res => {
        if (res.code == 1) {
          uni.setStorageSync('is_push_follow', data.val)
        }
        this.followLoading = false
      })
    },
    confirm() {
      let data = {
        type: 'msg',
        val: this.checkedIsPushMsg ? 1 : 0
      }
      this.post('/api/client/pushSet', data, true).then(res => {
        if (res.code == 1) {
          this.$u.toast('设置成功')
          uni.setStorageSync('is_push_msg', data.val)
        }
      })
      let data1 = {
        type: 'follow',
        val: this.checkedIsPushFollow ? 1 : 0
      }
      this.post('/api/client/pushSet', data1, true).then(res => {
        if (res.code == 1) {
          uni.setStorageSync('is_push_follow', data1.val)
        }
      })
      let data2 = {
        type: 'news',
        val: this.checkedIsPushNews ? 1 : 0
      }
      this.post('/api/client/pushSet', data2, true).then(res => {
        if (res.code == 1) {
          uni.setStorageSync('is_push_news', data2.val)
        }
      })
    },
    dian: function () {
      this.post('api/index/site', '', true).then(res => {
        console.log(res)
        if (res.code == 1) {
          this.mobile = res.data.service_mobile
        }
      })
    },
    // 增加访问量
    yang: function () {
      this.post('api/homepage/pageviews', { path: '/subpages/pages/lianxi' }, true).then(res => {
        console.log(res)
      })
    },
    back: function () {
      uni.navigateBack({
        delta: 1
      })
    },
    // 问题列表
    wenti: function () {
      this.post('api/index/problem', '', true).then(res => {
        console.log(res)
        if (res.code == 1) {
          this.list = res.data
        }
      })
    },
    // 联系我们
    wo: function () {
      this.post('api/help/contact', '', true).then(res => {
        console.log(res)
        if (res.code == 1) {
          this.work = res.data
        }
      })
    },
    da: function () {
      this.$refs.callPhone.open(this.mobile)
      // uni.makePhoneCall({
      //   phoneNumber: that.mobile //仅为示例that.work.mobile
      // });
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #f5f5f5 !important;
  font-family: Demibold;
}
.gradient-bg {
  width: 100%;
  height: 366rpx;
  background: linear-gradient(180deg, #1fb0ac 0%, #f5f5f5 100%);
  position: relative;
  padding-top: 40rpx;
}
.wdbg {
  width: 100%;
  height: 566rpx;
}
.big-line {
  width: 100%;
  background-color: #ffffff;
  border-radius: 16rpx;
  box-sizing: border-box;
  margin-bottom: 50rpx;
}
.row-line {
  line-height: 120rpx;
  box-sizing: border-box;
  color: #333333;
  font-size: 28rpx;
  position: relative;
  border-bottom: 1px solid #eeeeee;
  width: 100%;
  margin: 0 auto;
}
.arrow {
  color: #cccccc;
}
.slot-wrap {
  display: flex;
  align-items: center;
  /* 如果您想让slot内容占满整个导航栏的宽度 */
  /* flex: 1; */
  /* 如果您想让slot内容与导航栏左右有空隙 */
  padding: 0 30rpx;
}
button {
  border: none;
  background-color: none;
}
button[type="default"] {
  border: none;
  background-color: none;
}
button:after {
  border: none;
  background-color: none;
}
</style>

